<template>
<!--礼卡结账界面-->
<el-dialog :title="$t('collectmoney.dialog.giftCardCheckout.title')" :visible.sync="dialogFormVisible" width="300" :close-on-click-modal="false" @close="closeDialog">
    <el-form label-position="left" label-width="150px" :model="checkoutForm" ref="checkoutForm" :rules="checkoutFormRules">
        <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.orderPrice')">
            <el-input v-model="checkoutForm.orderPrice" disabled></el-input>
        </el-form-item>
        <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.giftCardNum')" prop="giftcardNo">
            <el-input v-model="checkoutForm.giftcardNo" @change="changeGiftcardNoEvent"></el-input>
        </el-form-item>
        <template v-if="showValidGiftcardData">
            <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.giftCardBalance')">
                <el-col :span="18">
                    <el-form-item>
                        <el-input v-model="checkoutForm.giftcardBalance" disabled></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item>
                        <el-button class="button" @click="handleConsumeRecordEnvent" type="primary">{{$t('collectmoney.dialog.giftCardCheckout.form.cardConsumeBtn')}}</el-button>
                    </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.clientName')">
                <el-input v-model="checkoutForm.clientName" disabled></el-input>
            </el-form-item>
            <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.clientPhone')">
                <el-input v-model="checkoutForm.clientPhone" disabled></el-input>
            </el-form-item>
            <template v-if="checkoutForm.missMoney<0">
                <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.missMoney')">
                    <el-input v-model="checkoutForm.missMoney" disabled></el-input>
                </el-form-item>
                <el-form-item :label="$t('collectmoney.dialog.giftCardCheckout.form.payWay')">
                    <el-radio v-model="checkoutForm.payWay" label="1" border size="medium">{{$t("collectmoney.dialog.giftCardCheckout.form.payWayCash")}}</el-radio>
                    <el-radio v-model="checkoutForm.payWay" label="2" border size="medium">{{$t("collectmoney.dialog.giftCardCheckout.form.payWayCreditCard")}}</el-radio>
                </el-form-item>
            </template>
        </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">{{$t("setting.route.shopSetDialog.dialogCancel")}}</el-button>
        <el-button type="primary" @click="checkoutOrderEvent">{{$t("collectmoney.dialog.giftCardCheckout.checkoutBtn")}}</el-button>
    </div>
    <giftcardPayrecord ref="giftcardPayrecord"></giftcardPayrecord>
</el-dialog>
</template>

<script>
import util from '../../common/js/util';
import GiftcardPayrecord from '../../components/giftcardPayrecord.vue';
import {
    getGiftcardDetailByCardno,
    addOrder
} from '../../api/api';
import {
    getLodop
} from '../../common/js/LodopFuncs';
export default {
    components: {
        giftcardPayrecord: GiftcardPayrecord
    },
    data() {
        return {
            order: {

            },
            checkoutForm: {
                orderPrice: 0,
                giftcardNo: "",
                giftCardId: "",
                giftcardBalance: 0,
                clientName: "",
                clientPhone: "",
                missMoney: 0,
                payWay: 0
            },
            checkoutFormRules: {
                giftcardNo: [{
                    required: true,
                    message: this.$t("collectmoney.dialog.giftCardCheckout.form.validMsg.noinputGiftCard"),
                    trigger: 'blur'
                }]
            },
            showValidGiftcardData: false,
            dialogFormVisible: false,
            addLoading: false
        }
    },
    mounted() {},
    methods: {
        //显示界面
        showPage(para) {
            this.order = para;
            this.dialogFormVisible = true;
        },
        checkoutOrderEvent() {
            this.$refs.checkoutForm.validate((valid) => {
                        if (valid) {
                            //先判断是否输入卡号，卡号的余额是否满足，如果不足，是否选择了现金或者信用卡支付
                            if (this.checkoutForm.giftcardBalance <= 0) {
                                this.$message({
                                    message: this.$t("collectmoney.dialog.giftCardCheckout.form.validMsg.invalidGiftCard"),
                                    type: 'error'
                                });
                                return;
                            }
                            if (this.checkoutForm.missMoney < 0 && this.checkoutForm.payWay == 0) {
                                if (this.checkoutForm.payWay == 0) {
                                    this.$message({
                                        message: this.$t("collectmoney.dialog.giftCardCheckout.form.validMsg.noMissMoneyPayway"),
                                        type: 'error'
                                    });
                                    return;
                                }
                            }
                            this.$confirm(this.$t("collectmoney.dialog.giftCardCheckout.checkoutOkDialog.content"), this.$t("collectmoney.dialog.giftCardCheckout.checkoutOkDialog.title"), {
                                type: 'warning'
                            }).then(() => {
                                    //处理业务
                                    let para = this.order;

                                    if (this.checkoutForm.missMoney < 0 && this.checkoutForm.payWay != 0) {
                                        let payRecord_giftCard = {};
                                        payRecord_giftCard.payWay = 3;
                                        payRecord_giftCard.payMoney = this.checkoutForm.giftcardBalance;
                                        payRecord_giftCard.giftCardId = this.checkoutForm.giftCardId;
                                        para.payRecords.push(payRecord_giftCard);

                                        let payRecord_missMoney = {};
                                        payRecord_missMoney.payWay = this.checkoutForm.payWay;
                                        payRecord_missMoney.payMoney = Math.abs(this.checkoutForm.missMoney);
                                        para.payRecords.push(payRecord_missMoney);
                                    } else {
                                        let payRecord_giftCard = {};
                                        payRecord_giftCard.payWay = 3;
                                        payRecord_giftCard.payMoney = this.checkoutForm.orderPrice;
                                        payRecord_giftCard.giftCardId = this.checkoutForm.giftCardId;
                                        para.payRecords.push(payRecord_giftCard);
                                    }

                                    addOrder(para).then(res => {
                                            let {
                                                msg,
                                                code
                                            } = res;
                                            if (code !== "0") {
                                                this.$message({
                                                    message: msg,
                                                    type: 'error'
                                                });
                                            } else {
                                                try {
                                                    //热敏小票打印
                                                    let LODOP = getLodop(); //调用getLodop获取LODOP对象
                                                    if (typeof (LODOP) != "undefined") {
                                                        var initContentTop = 50; //初始化打印主内容的TOP高度
                                                        var lineHeight = 20; //每行的高度
                                                        var dateStr = util.formatDate.format(new Date(), 'MM-dd-yyyy'); //美国国家标准时间格式(月日年)
                                                        var timeStr = util.formatDate.format(new Date(), 'hh:mm:ss');
                                                        //#######结账-订单-打印小票##########
                                                        LODOP.PRINT_INITA(0, 0, 580, 793, "打印订单小票");
                                                        LODOP.SET_PRINT_PAGESIZE(1, 580, 1400, ""); //设置打印页大小(横向或纵向,宽度和高度)
                                                        LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
                                                        LODOP.ADD_PRINT_HTM(8, 0, "100%", "100%", "<div align=\"center\" style=\"font-weight:bold;\">" + res.data.shop.shopName + "</div>");
                                                        LODOP.ADD_PRINT_LINE(40, -7, 40, 377, 4, 0);
                                                        LODOP.ADD_PRINT_HTM(initContentTop, 0, "100%", "100%", "<div style=\"font-size:11px;\">" + this.$t('collectmoney.print.orderNo') + res.data.orderNum + "</div>");
                                                        LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * 1, 0, "100%", "100%", "<div style=\"font-size:11px;\"><div style=\"float:left;\">" + this.$t('collectmoney.print.checkoutStaff') + res.data.checkoutUserName + "</div><div style=\"float:right;\">" + this.$t('collectmoney.print.serviceStaff') + res.data.serviceStaffName + "</div></div>");
                                                        LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * 2, 0, "100%", "100%", "<div style=\"font-size:11px;\"><div style=\"float:left;\">" + this.$t("collectmoney.print.date") + dateStr + "</div><div style=\"float:right;\">" + this.$t("collectmoney.print.time") + timeStr + "</div></div>");
                                                        LODOP.ADD_PRINT_LINE(initContentTop + lineHeight * 3, -7, initContentTop + lineHeight * 3, 377, 4, 0);
                                                        LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * 4, 0, "100%", "100%", "<ul style=\"font-size:11px;list-style:none;font-weight:bold;margin:0;padding:0;\"><li style=\"list-style:none;float:left;width:40%;text-align:left;\">" + this.$t('collectmoney.print.projectName') + "</li><li style=\"list-style:none;float:left;width:20%;text-align:right;\">" + this.$t('collectmoney.print.projectPrice') + "</li><li style=\"list-style:none;float:left;width:20%;text-align:right;\">" + this.$t('collectmoney.print.projectNumber') + "</li><li style=\"list-style:none;float:left;width:20%;text-align:right;\">" + this.$t('collectmoney.print.projectMoney') + "</li></ul>");
                                                        let projectTypeNum = this.$parent.shopCartList.length;
                                                        let projectNumber = 0;
                                                        for (let i = 0; i < projectTypeNum; i++) {
                                                            let obj = this.$parent.shopCartList[i];
                                                            projectNumber = projectNumber + obj.num;
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + i), 0, "100%", "100%", "<ul style=\"font-size:11px;list-style:none;margin:0;padding:0;\"><li style=\"list-style:none;float:left;width:40%;text-align:left;\">" + obj.projectName + "</li><li style=\"list-style:none;float:left;width:20%;text-align:right;\">" + obj.price + "</li><li style=\"list-style:none;float:left;width:20%;text-align:right;\">" + obj.num + "</li><li style=\"list-style:none;float:left;width:20%;text-align:right;\">" + util.math.multi(obj.num, obj.price) + "</li></ul>");
                                                            }
                                                            LODOP.ADD_PRINT_LINE(initContentTop + lineHeight * (5 + projectTypeNum), -7, initContentTop + lineHeight * (5 + projectTypeNum), 377, 4, 0);
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 1), 0, "100%", "100%", "<div style=\"font-size:11px;\">" + util.StringFormat(this.$t('collectmoney.print.totalProjectNumber'), projectTypeNum, projectNumber) + "</div>");
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2), 0, "100%", "100%", "<div style=\"font-size:14px;text-align:right;\">" + this.$t('collectmoney.print.totalMoney') + "<span style=\"font-size:14px;font-weight:bold;\">" + this.$parent.shopCartPrice + "</span></div>");
                                                            let printPayRecordNum = 0;
                                                            for (let i = 0; i < para.payRecords.length; i++) {
                                                                let payRecord = para.payRecords[i];
                                                                if (1 == payRecord.payWay) {
                                                                    //现金
                                                                    printPayRecordNum++;
                                                                    LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum), 0, "100%", "100%", "<div style=\"font-size:11px;text-align:right;\">" + this.$t('collectmoney.print.cashPay') + payRecord.payMoney + "</div>");
                                                                }
                                                                if (2 == payRecord.payWay) {
                                                                    //信用卡
                                                                    printPayRecordNum++;
                                                                    LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum), 0, "100%", "100%", "<div style=\"font-size:11px;text-align:right;\">" + this.$t('collectmoney.print.creditPay') + payRecord.payMoney + "</div>");
                                                                }
                                                                if (3 == payRecord.payWay) {
                                                                    //礼卡：礼卡卡号：
                                                                    printPayRecordNum++;
                                                                    LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum), 0, "100%", "100%", "<div style=\"font-size:11px;text-align:right;\">" + this.$t('collectmoney.print.giftPay') + payRecord.payMoney + "</div>");
                                                                }
                                                            }
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum + 1), 0, "100%", "100%", "<div style=\"height:1px;border-top:1px dashed #000;\"></div>");
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum + 2), 0, "100%", "100%", "<div style=\"font-size:11px;text-align:center;\">" + this.$t('collectmoney.print.shopPhone') + res.data.shop.phone + "</div>");
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum + 3), 0, "100%", "100%", "<div style=\"font-size:11px;text-align:left;\">" + res.data.shop.address + "</div>");
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum + 4), 0, "100%", "100%", "<div style=\"height:1px;border-top:1px dashed #000;\"></div>");
                                                            LODOP.ADD_PRINT_HTM(initContentTop + lineHeight * (5 + projectTypeNum + 2 + printPayRecordNum + 5), 0, "100%", "100%", "<div style=\"font-size:11px;text-align:center;\">" + res.data.shop.welcomeTip + "</div>");
                                                            LODOP.PRINT();
                                                        }
                                                    } catch (err) {
                                                        console.log(err);
                                                    }

                                                    this.closeDialog();
                                                    this.$router.push({
                                                        path: '/home'
                                                    });
                                                }
                                            });

                                    }).catch(() => {

                                });
                            }
                        });
                },
                changeGiftcardNoEvent() {

                    this.checkoutForm.giftCardId = "";
                    this.showValidGiftcardData = false;
                    this.checkoutForm.giftcardBalance = 0;
                    this.checkoutForm.clientName = 0;
                    this.checkoutForm.clientPhone = 0;
                    this.checkoutForm.missMoney = 0;
                    this.checkoutForm.payWay = 0;

                    let para = {
                        cardNo: this.checkoutForm.giftcardNo
                    };
                    getGiftcardDetailByCardno(para).then(res => {
                        let {
                            msg,
                            code
                        } = res;
                        if (code !== "0") {
                            this.$message({
                                message: msg,
                                type: 'error'
                            });
                        } else {
                            //卡号不存在提示
                            if (res.data == null) {
                                this.$message({
                                    message: this.$t("collectmoney.dialog.giftCardCheckout.form.validMsg.noExistGiftCard"),
                                    type: 'error'
                                });
                                return;
                            }
                            this.checkoutForm.giftCardId = res.data.id;
                            this.checkoutForm.giftcardBalance = res.data.balanceMoney;
                            this.checkoutForm.clientName = res.data.clientName;
                            this.checkoutForm.clientPhone = res.data.clientPhone;
                            this.showValidGiftcardData = true;
                            this.checkoutForm.missMoney = util.math.sub(this.checkoutForm.giftcardBalance, this.checkoutForm.orderPrice);
                        }
                    });
                },
                // 对话框关闭事件
                closeDialog() {
                    // 点击关闭 数据重置
                    this.$refs.checkoutForm.resetFields();

                    this.checkoutForm.giftCardId = "";
                    this.checkoutForm.giftcardNo = "";
                    this.showValidGiftcardData = false;
                    this.checkoutForm.giftcardBalance = 0;
                    this.checkoutForm.clientName = 0;
                    this.checkoutForm.clientPhone = 0;
                    this.checkoutForm.missMoney = 0;
                    this.checkoutForm.payWay = 0;

                    this.dialogFormVisible = false;
                    //父窗口的支付方式：礼卡的当前选中项给删除掉
                    this.$parent.payway = 0;

                },
                handleConsumeRecordEnvent() {
                    this.$refs.giftcardPayrecord.showPage(this.checkoutForm.giftCardId);
                }
        }
    }
</script>

<style scoped>
</style>
